package com.thomsonreuters.elektronunified;

import com.reuters.rfa.common.Client;
import com.reuters.rfa.common.Event;
import com.reuters.rfa.dictionary.FieldDictionary;
import com.reuters.rfa.omm.OMMElementEntry;
import com.reuters.rfa.omm.OMMElementList;
import com.reuters.rfa.omm.OMMMsg;
import com.reuters.rfa.omm.OMMNumeric;
import com.reuters.rfa.omm.OMMSeries;
import com.reuters.rfa.omm.OMMTypes;
import com.reuters.rfa.rdm.RDMDictionary;
import com.reuters.rfa.session.omm.OMMItemEvent;

class DictionaryClient implements Client 
{
    private RDMCallbacks mainApp;
    private FieldDictionary downloadedDictionary;
    private boolean fieldDictionaryComplete = false;
    private boolean enumDictionaryComplete = false;
    private int retryCount = 0;

    public DictionaryClient(RDMCallbacks mainApp) 
    {
        this.mainApp = mainApp;
        downloadedDictionary = FieldDictionary.create();
    }

    public void processEvent(Event event) 
    {

        OMMItemEvent ie = (OMMItemEvent) event;
        OMMMsg respMsg = ie.getMsg();

        if (respMsg.getDataType() == OMMTypes.SERIES)  
        {
            processDictionary(respMsg);
        }
        else
        {
           retryCount++;
        }
        
        if (retryCount == 5)
        {
        	mainApp.onDictionariesInCompleted();
        }
        	

    }

    private void processDictionary(OMMMsg msg) 
    {
        OMMSeries dictionaryData = (OMMSeries) msg.getPayload();

        int dictionaryType = getDictionaryType(dictionaryData);

        if (dictionaryType == RDMDictionary.Type.FIELD_DEFINITIONS) 
        {
            FieldDictionary.decodeRDMFldDictionary(downloadedDictionary, dictionaryData);
            
            if (msg.isSet(OMMMsg.Indication.REFRESH_COMPLETE))
            {
                fieldDictionaryComplete = true;
            }
        }
        else if (dictionaryType == RDMDictionary.Type.ENUM_TABLES) 
        {
            FieldDictionary.decodeRDMEnumDictionary(downloadedDictionary, dictionaryData);
            
            if (msg.isSet(OMMMsg.Indication.REFRESH_COMPLETE)) 
            {
                enumDictionaryComplete = true;
            }
        }
 
        boolean isInitialImage =
                msg.getMsgType() == OMMMsg.MsgType.REFRESH_RESP &&
                msg.getRespTypeNum() == OMMMsg.RespType.SOLICITED;
        if (isInitialImage && fieldDictionaryComplete && enumDictionaryComplete)
        {
            mainApp.onDictionariesCompleted(downloadedDictionary);
            
        }
    }

    private static int getDictionaryType(OMMSeries series) 
    {
        int dictionaryType = RDMDictionary.Type.UNSPECIFIED;
        if (series.has(OMMSeries.HAS_SUMMARY_DATA)) 
        {
            OMMElementList summaryData = (OMMElementList) series.getSummaryData();
            OMMElementEntry entry = summaryData.find(RDMDictionary.Summary.Type);
            
            if (entry != null)
            {
                dictionaryType = (int) ((OMMNumeric) entry.getData()).toLong();
            }
        }
        return dictionaryType;
    }
}